﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net.MVC;
using Buffalo.SMECommon.DAL;
using Buffalo.SMECommon.Enum;
using Buffalo.Administration.Enum;
using Buffalo.Administration.DAL;
using Buffalo.Lib.Common;
using Buffalo.SMECommon.DAL.Models;

namespace Buffalo.Web.Areas.SMECommon.Controllers
{
    public class CatalogController : BaseController
    {
        #region Bank
        public AjaxStoreResult GetListBank(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<Bank> list = new CatalogDAL().GetListBank(limit, start, dir, sort, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new Bank { ID = GeneralConstant.BLANK_ITEM, Name = "..." });

                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SaveBanks()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SaveBanks(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult BankList()
        {
            return CheckPermission("Bank");
        }

        #endregion

        #region PartnerCategory
        public AjaxStoreResult GetListPartnerCategory(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<PartnerCategory> list = new CatalogDAL().GetListPartnerCategory(limit, start, dir, sort, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new PartnerCategory { ID = GeneralConstant.BLANK_ITEM, Name = "..." });

                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SavePartnerCategories()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SavePartnerCategories(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult PartnerCategoryList()
        {
            return CheckPermission("PartnerCategory");
        }

        #endregion

        #region Nation
        public AjaxStoreResult GetListNation(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<Nation> list = new CatalogDAL().GetListNation(limit, start, dir, sort, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new Nation { ID = GeneralConstant.BLANK_ITEM, Name = "..." });

                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SaveNations()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SaveNations(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult NationList()
        {
            return CheckPermission("Nation");
        }

        #endregion

        #region Religion
        public AjaxStoreResult GetListReligion(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<Religion> list = new CatalogDAL().GetListReligion(limit, start, dir, sort, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new Religion { ID = GeneralConstant.BLANK_ITEM, Name = "..." });

                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SaveReligions()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SaveReligions(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult ReligionList()
        {
            return CheckPermission("Religion");
        }

        #endregion

        #region Origin
        public AjaxStoreResult GetListOrigin(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<Origin> list = new CatalogDAL().GetListOrigin(limit, start, dir, sort, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new Origin { ID = GeneralConstant.BLANK_ITEM, Name = "..." });

                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SaveOrigins()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SaveOrigins(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult OriginList()
        {
            return CheckPermission("Origin");
        }

        #endregion

        #region PaymentTerm
        public AjaxStoreResult GetListPaymentTerm(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<PaymentTerm> list = new CatalogDAL().GetListPaymentTerm(limit, start, dir, sort, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new PaymentTerm { ID = GeneralConstant.BLANK_ITEM, Name = "..." });

                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SavePaymentTerms()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SavePaymentTerms(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult PaymentTermList()
        {
            return CheckPermission("PaymentTerm");
        }

        #endregion

        #region PaymentType
        public AjaxStoreResult GetListPaymentType(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<PaymentType> list = new CatalogDAL().GetListPaymentType(limit, start, dir, sort, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new PaymentType { ID = GeneralConstant.BLANK_ITEM, Name = "..." });

                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SavePaymentTypes()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SavePaymentTypes(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult PaymentTypeList()
        {
            return CheckPermission("PaymentType");
        }

        #endregion

        #region Position
        public AjaxStoreResult GetListPosition(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<Position> list = new CatalogDAL().GetListPosition(limit, start, dir, sort, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new Position { ID = GeneralConstant.BLANK_ITEM, Name = "..." });

                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SavePositions()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SavePositions(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult PositionList()
        {
            return CheckPermission("Position");
        }

        #endregion

        #region School
        public AjaxStoreResult GetListSchool(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<School> list = new CatalogDAL().GetListSchool(limit, start, dir, sort, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new School { ID = GeneralConstant.BLANK_ITEM, Name = "..." });

                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SaveSchools()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SaveSchools(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult SchoolList()
        {
            return CheckPermission("School");
        }

        #endregion

        #region OrderType
        public AjaxStoreResult GetListOrderType(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<OrderType> list = new CatalogDAL().GetListOrderType(limit, start, dir, sort, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new OrderType { ID = GeneralConstant.BLANK_ITEM, Name = "..." });

                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SaveOrderTypes()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SaveOrderTypes(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult OrderTypeList()
        {
            return CheckPermission("OrderType");
        }

        #endregion

        #region SalaryFactor
        public AjaxStoreResult GetListSalaryFactor(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<SalaryFactor> list = new CatalogDAL().GetListSalaryFactor(limit, start, dir, sort, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new SalaryFactor { ID = GeneralConstant.BLANK_ITEM, GradeName = "..." });

                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SaveSalaryFactors()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SaveSalaryFactors(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult SalaryFactorList()
        {
            return CheckPermission("SalaryFactor");
        }

        #endregion

        #region SalesMethod
        public AjaxStoreResult GetListSalesMethod(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<SalesMethod> list = new CatalogDAL().GetListSalesMethod(limit, start, dir, sort, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new SalesMethod { ID = GeneralConstant.BLANK_ITEM, Name = "..." });

                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SaveSalesMethods()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SaveSalesMethods(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult SalesMethodList()
        {
            return CheckPermission("SalesMethod");
        }

        #endregion

        #region ShippingMethod
        public AjaxStoreResult GetListShippingMethod(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<ShippingMethod> list = new CatalogDAL().GetListShippingMethod(limit, start, dir, sort, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new ShippingMethod { ID = GeneralConstant.BLANK_ITEM, Name = "..." });

                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SaveShippingMethods()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SaveShippingMethods(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult ShippingMethodList()
        {
            return CheckPermission("ShippingMethod");
        }

        #endregion

        #region Country
        public AjaxStoreResult GetListCountry(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<Country> list = new CatalogDAL().GetListCountry(limit, start, dir, sort, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new Country { ID = GeneralConstant.BLANK_ITEM, Name = "..." });

                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SaveCountries()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SaveCountries(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult CountryList()
        {
            return CheckPermission("Country");
        }

        #endregion

        #region Industry
        public AjaxStoreResult GetListIndustry(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<Industry> list = new CatalogDAL().GetListIndustry(limit, start, dir, sort, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new Industry { ID = GeneralConstant.BLANK_ITEM, Name = "..." });

                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SaveIndustries()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SaveIndustries(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult IndustryList()
        {
            return CheckPermission("Industry");
        }

        #endregion

        #region Currency
        public AjaxStoreResult GetListCurrency(int limit, int start, string dir, string sort, string withDefaultItem, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();
                List<Currency> list = new CatalogDAL().GetListCurrency(limit, start, dir, sort, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new Currency { ID = GeneralConstant.BLANK_ITEM, Name = "..." });

                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxStoreResult SaveCurrencies()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);

            try
            {
                new CatalogDAL().SaveCurrencies(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult CurrencyList()
        {
            return CheckPermission("Currency");
        }

        #endregion
    }
}
